'use strict';
module.exports = {
	/**
	 * XXXnameXXX
	 * @url client/user/kh/createCashOut 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid, balance } = data;
		let res = { code: 0, msg: "" };
		// 业务逻辑开始-----------------------------------------------------------
    // 可写与数据库的交互逻辑等等
     let dbName = 'uni-id-users'
     let curUser = await vk.baseDao.findById({
       dbName,
       id: uid
     });
     if (!curUser) {
       res.code = -1
       res.msg = '用户不存在'
       return res
     }
     if (curUser.balance < balance) {
       res.code = -1
       res.msg = '余额不足'
       return res
     }
     
     let userCashOut = await vk.baseDao.updateById({
       dbName,
       id: uid,
       dataJson: {
         balance: 0,
       }
     })
     
    let cashOutDbName = 'ai-cash-out'
    const charge = parseInt(balance * 0.05)
    const received = parseInt(balance - charge)
     
    let cashOut = await vk.baseDao.add({
       dbName: cashOutDbName,
       id: uid,
       dataJson:{
         user_id: uid,
         amount: balance,
         charge,
         received,
         status: 1,
         created_at: new Date().getTime()
       },
        getUpdateData:true
     });
     console.log(cashOut)
     res.msg = '提现申请成功，请等待打款'



		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}
